<template>
    <div class="edit-popup">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="550px" @confirm="handleSubmit"
            @close="handleClose">
            <el-form ref="formRef" :model="formData" label-width="90px" :rules="formRules">

                <el-form-item label="产品类型" prop="num">
                    <el-select v-model="formData.product_type_id" value-key="" placeholder="" clearable filterable>
                        <el-option v-for="item in state.productTypeList" :key="item.id" :label="item.name"
                            :value="item.id">
                        </el-option>
                    </el-select>

                </el-form-item>
                <el-form-item label="机型" prop="model_id">
                    <el-select v-model="formData.model_id" value-key="" placeholder="" clearable filterable>
                        <el-option v-for="item in state.modelList" :key="item.id" :label="item.spec" :value="item.id">
                        </el-option>
                    </el-select>

                </el-form-item>

                <el-form-item label="次数" prop="num">
                    <el-input v-model="formData.num" clearable placeholder="请输入次数" />
                </el-form-item>



                <el-form-item label="价格" prop="price">
                    <el-input v-model="formData.price" clearable placeholder="请输入价格" />
                </el-form-item>

                <el-form-item label="年限" prop="year">
                    <el-input v-model="formData.year" clearable placeholder="请输入机型" />
                </el-form-item>

                
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup name="productEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiProductAdd, apiProductEdit, apiProductDetail } from '@/api/product'
import { apiProductTypeAll } from '@/api/product_type'

import { timeFormat } from '@/utils/util'
import type { PropType } from 'vue'
import { apiModelAll } from '@/api/model'
defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')


const state = reactive({
    productTypeList: [],
    modelList: []
})





// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑产品' : '新增产品'
})

// 表单数据
const formData = reactive({
    id: '',
    name: '',
    img: '',
    status: '',
    num: 1,
    commission: '',
    start_date: '',
    end_date: '',
    model_id: '',
    product_type_id: '',
    price: '',
    year: 1
})


// 表单验证
const formRules = reactive<any>({
    // name: [{
    //     required: true,
    //     message: '请输入名称',
    //     trigger: ['blur']
    // }],

})


// 获取详情
const setFormData = async (data: Record<any, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }


}

const getDetail = async (row: Record<string, any>) => {
    const data = await apiProductDetail({
        id: row.id
    })
    setFormData(data)
}


// 提交按钮
const handleSubmit = async () => {
    await formRef.value?.validate()
    const data = { ...formData, }
    mode.value == 'edit'
        ? await apiProductEdit(data)
        : await apiProductAdd(data)
    popupRef.value?.close()
    emit('success')
}

//打开弹窗
const open = async (type = 'add') => {
    state.productTypeList = await apiProductTypeAll({})
    state.modelList = await apiModelAll({})
    mode.value = type
    popupRef.value?.open()
}

// 关闭回调
const handleClose = () => {
    emit('close')
}
onMounted(() => {

})



defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
